﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ThridGroup.ERP.DTO.HumanResourcesDto;
using ThridGroup.ERP.DTO.ProductionSystem;
using ThridGroup.ERP.HumanResources;

namespace ThridGroup.ERP.Web.Controllers.HumanResources
{ 
    /// <summary>
    /// 入职申请控制器
    /// </summary>
    [Route("/OnboardingReview/[action]")]
    public class OnboardingReviewController : Controller
    {
        private readonly IOnboardingReviewServices _onboardingReviewService;

        public OnboardingReviewController(IOnboardingReviewServices onboardingReviewService)
        {
            _onboardingReviewService = onboardingReviewService;
        }

        /// <summary>
        /// 添加视图
        /// </summary>
        /// <returns></returns>
        public IActionResult OnboardingReviewView()
        {
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> AddOnboardingReview(OnboardingReviewDto dto)
        {
            try
            {
                var id = await _onboardingReviewService.OnboardingReviewAsync(dto);
                return Json(new { code = 0, msg = "添加成功", id });
            }
            catch (Exception ex)
            {
                return Json(new { code = 1, msg = "添加失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 显示列表视图
        /// </summary>
        /// <returns></returns>
        public IActionResult OnboardingReviewListView()
        {
            return View();
        }

        


        [HttpGet]
        public async Task<IActionResult> GetOnboardingReviewList(int page, int limit, string personName = null, string interviewPeople = null)
        {
            var result = await _onboardingReviewService.GetOnboardingReviewList(page, limit, personName, interviewPeople);
            return Json(result);
        }

        [HttpPost]
        public async Task<IActionResult> DeleteOnboardingReview(int id)
        {
            await _onboardingReviewService.DeleteOnboardingReviewAsync(id);
            return Json(new { success = true });
        }

        [HttpPost]
        public async Task<IActionResult> BatchDeleteOnboardingReview(List<int> ids)
        {
            await _onboardingReviewService.BatchDeleteOnboardingReviewAsync(ids);
            return Json(new { success = true });
        }

        [HttpGet]
        public async Task<IActionResult> GetOnboardingReviewById(int id)
        {
            var dto = await _onboardingReviewService.GetOnboardingReviewByIdAsync(id);
            return Json(dto);
        }





        /// <summary>
        /// 修改入职申请视图
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdateOnboardingReview()
        {
            return View();
        }




        /// <summary>
        /// 修改入职申请
        /// </summary>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> UpdateOnboardingReview(OnboardingReviewDto input)
        {
            if (!ModelState.IsValid)
            {
                return Json(new { code = 1, msg = "请填写必填项" });
            }

            try
            {
                var id = await _onboardingReviewService.UpdateOnboardingReviewAsync(input);
                return Json(new { code = 0, msg = "修改成功", id });
            }
            catch (Exception ex)
            {
                // 返回详细异常信息，便于前端排查
                return Json(new { code = 1, msg = "修改失败：" + ex.ToString() });
            }
        }

    }
}
